import {isWechat} from '@utils'
import api from '@api'

const state = {
  location: {
    name: '',
    latitude: '',
    longitude: '',
    disable: false
  },
  isLogin: false,
  account: {
    data: null
  }
}

const getters = {
  ['getLocation'] (state) {
    return state.location;
  },
  ['getAccount'] (state) {
    return state.account;
  },
  ['isLogin'] (state) {
    return state.isLogin;
  }
}

const actions = {
  async ['getLocation'] ({commit}, {latitude, longitude}) {
    let {data:{address}} = await api.get('api/v1/Area/getDetailAddressByIp.ashx', {location: longitude + ',' + latitude});
    commit('updateLocationName', address)
  },
  async ['getUser'] ({commit}) {
    let {data: {userInfo, StatusCode}} = await api.get('api/v1/UserInfo/userInfo.ashx');
    if(StatusCode === '200') {
      commit('setUser', userInfo[0])
      commit('setLoginState', true)
    } else {
      commit('setLoginState', false)
    }
    return StatusCode
  },
  async ['log'] ({commit}, loginState) {
    let {data: { StatusCode}} = await api.get('api/v2/Log/loginState.ashx');
    if (StatusCode === 200) {
      commit('setLoginState', true)
    } else {
      window.location.href = 'http://www.gogagoga.cc/mwxLogin.aspx?url='+ location.href;
      commit('setLoginState', false)
    }
    return StatusCode;
  }
}
const mutations = {
  ['updateLocationName'] (state, name) {
    state.location.name = name;
  },
  ['uodateLocationZB'] (state, {latitude, longitude}) {
    state.location.latitude = latitude;
    state.location.longitude = longitude;
  },
  ['updateLocationDisable'] (state, isDisable) {
    state.location.disable = isDisable
  },
  ['setUser'] (state, data) {
    state.account.data = data
  },
  ['setLoginState'] (state, isLogin) {
    state.isLogin = isLogin
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
